package com.ruoyi.forum.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.forum.mapper.ForumStudentMapper;
import com.ruoyi.forum.domain.ForumStudent;
import com.ruoyi.forum.service.IForumStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-02
 */
@Service
public class ForumStudentServiceImpl implements IForumStudentService 
{
    @Autowired
    private ForumStudentMapper forumStudentMapper;

    @Autowired
    private SysUserServiceImpl userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询学生管理
     * 
     * @param id 学生管理主键
     * @return 学生管理
     */
    @Override
    public ForumStudent selectForumStudentById(Long id)
    {
        ForumStudent forumStudent = forumStudentMapper.selectForumStudentById(id);
        forumStudent.setPassword(null);
        return forumStudent;
    }

    /**
     * 查询学生管理列表
     * 
     * @param forumStudent 学生管理
     * @return 学生管理
     */
    @Override
    public List<ForumStudent> selectForumStudentList(ForumStudent forumStudent)
    {
        return forumStudentMapper.selectForumStudentList(forumStudent);
    }

    /**
     * 新增学生管理
     * 
     * @param forumStudent 学生管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertForumStudent(ForumStudent forumStudent)
    {

        SysUser user = new SysUser();
        user.setDeptId(forumStudent.getDeptId());
        user.setUserName(forumStudent.getStudentName());
        user.setPassword(forumStudent.getPassword());
        user.setNickName(forumStudent.getNickName());
        user.setSex(forumStudent.getSex());
        user.setPhonenumber(forumStudent.getPhonenumber());
        user.setEmail(forumStudent.getEmail());
        user.setAvatar(forumStudent.getAvatar());
        user.setStatus(forumStudent.getStatus());
        user.setDelFlag(forumStudent.getDelFlag());
        user.setRemark(forumStudent.getRemark());
        user.setCreateBy(forumStudent.getCreateBy());
        int i = userService.insertUser(user);
        System.out.println(i);
        if(i>0){
            forumStudent.setStudentId(user.getUserId());
            i = forumStudentMapper.insertForumStudent(forumStudent);
            //新增学生用户与角色对象信息
            SysRole sysRole = new SysRole();
            sysRole.setRoleName("学生");
//            List<SysRole> sysRoles = roleService.selectRoleList(sysRole);
            List<SysRole> sysRoles = roleMapper.selectRoleList(sysRole);
            if(sysRoles!=null&&sysRoles.size()>0) {
                SysRole role = sysRoles.get(0);
                userService.insertUserRole(forumStudent.getStudentId(), new Long[]{role.getRoleId()});
            }
        }
        return i;
    }

    /**
     * 修改学生管理
     * 
     * @param forumStudent 学生管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateForumStudent(ForumStudent forumStudent)
    {
        SysUser user = new SysUser();
        user.setUserId(forumStudent.getStudentId());
        user.setUserName(forumStudent.getStudentName());
        user.setPassword(forumStudent.getPassword());
        user.setNickName(forumStudent.getNickName());
        user.setSex(forumStudent.getSex());
        user.setPhonenumber(forumStudent.getPhonenumber());
        user.setEmail(forumStudent.getEmail());
        user.setAvatar(forumStudent.getAvatar());
        user.setStatus(forumStudent.getStatus());
        user.setDelFlag(forumStudent.getDelFlag());
        user.setRemark(forumStudent.getRemark());
//        user.setCreateBy(forumStudent.getCreateBy());
//        userService.updateUser(user);
        //sql语句已经自动更新修改时间。
        int userResult = userMapper.updateUser(user);
        //判断学生表是否有该用户的关联数据
        ForumStudent forumStudent1 = forumStudentMapper.selectForumStudentAloneById(forumStudent.getStudentId());
        if (forumStudent1==null){
            forumStudent1 = new ForumStudent();
            forumStudent1.setStudentId(forumStudent.getStudentId());
            forumStudentMapper.insertForumStudent(forumStudent1);
        }
        if(forumStudent.getSchoolId()!=null||forumStudent.getCollegeId()!=null||
        forumStudent.getSpecialtyId()!=null||forumStudent.getGrade()!=null){
            userResult = forumStudentMapper.updateForumStudent(forumStudent);
        }
        return userResult;
    }

    /**
     * 修改学生密码管理
     *
     * @param forumStudent 学生管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateForumStudentPassword(ForumStudent forumStudent)
    {
        SysUser user = new SysUser();
        user.setUserId(forumStudent.getStudentId());
//        user.setUserName(forumStudent.getStudentName());
        user.setPassword(forumStudent.getPassword());
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除学生管理
     * 
     * @param ids 需要删除的学生管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteForumStudentByIds(Long[] ids)
    {
        //删除学生在用户表的信息
        userService.deleteUserByIds(ids);
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(ids);
        //删除学生表的信息
        return forumStudentMapper.deleteForumStudentByIds(ids);
    }

    /**
     * 删除学生管理信息
     * 
     * @param id 学生管理主键
     * @return 结果
     */
    @Override
    public int deleteForumStudentById(Long id)
    {
        return forumStudentMapper.deleteForumStudentById(id);
    }
}
